package com.iss.servlet;

import com.iss.bean.Consumer;
import com.iss.service.UserService;
import com.iss.service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/login")
public class LoginServlet extends HttpServlet {

    private UserService service = new UserServiceImpl();
     @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        try {
            String username=req.getParameter("username");
            String password=req.getParameter("password");
            Consumer consumer= service.getUsersByUserIdAndPassword(username,password);
            if(consumer!=null && consumer.getUserId()!=null && !consumer.getUserId().equals("")) {
                req.getSession().setAttribute("consumer",consumer);
                resp.sendRedirect("index.jsp");
            } else {
                req.setAttribute("msg","用户名或密码错误");
                req.getRequestDispatcher("login.jsp").forward(req,resp);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
